#include <bits/stdc++.h>
using namespace std;
class Node{
public:
    int data;
    Node* pNext;
    Node(int d,Node* n = nullptr)
    {
        data = d;
        pNext = n;
    }
};
//获取链表的长度
int GetLengthOfLink(Node* head)
{
    int count = 0;
    Node* Current_Node = head;
    while(Current_Node!=nullptr)
    {
        Current_Node = Current_Node->pNext;
        count++;
    }
    return count;
}
Node* GetFirstNode(Node* head1,Node* head2)
{
    //初始化更长链表和更短链表
    Node* Longer_link = head1;
    Node* Shorter_link = head2;

    unsigned int length_l = GetLengthOfLink(head1);
    unsigned int length_s = GetLengthOfLink(head2);
    unsigned int diff_link = abs(length_l-length_s);
    //如果初始化的长度不对，则重新初始化
    if(length_s>length_l)
    {
        Longer_link = head2;
        Shorter_link = head1;
    }
    //长链表先移动diff_link位
    for(int i=0;i<diff_link;i++)
        Longer_link = Longer_link->pNext;
    for(int i=0;i<length_s;i++)
    {
        if(Longer_link->data == Shorter_link->data)
            return Longer_link;
        else
        {
            Longer_link = Longer_link->pNext;
            Shorter_link = Shorter_link->pNext;
        }
    }
    return nullptr;
}
int main()
{
    Node* a = new Node(3);
    Node* b = new Node(4);
    Node* c = new Node(5);
    Node* d = new Node(6);

    Node* b1 = new Node(2);
    a->pNext = b;
    b->pNext = c;
    c->pNext = d;
    b1->pNext = c;
    Node* First_node = GetFirstNode(a,b1);
    if(First_node!=nullptr)
        cout<<First_node->data;
    delete a,b,c,d,b1;
}